package com.bsj.sipgateway.sip.session;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.bsj.sipgateway.sip.bean.TempData;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 保存会话的，设备发送数据的时候知道他是哪个设备
 */
@Component
public class CallIdManager {
    //根据callId保存deviceId
    private static TimedCache<String, TempData> callIdMap = CacheUtil.newTimedCache(30000);

    @PostConstruct
    public void init() {
        callIdMap.schedulePrune(1000);
    }

    public static void put(String callId, String deviceId, String deviceIp, Integer devicePort, String ssrc) {
        callIdMap.put(callId, new TempData(deviceId, deviceIp, devicePort, ssrc));
    }

    public static TempData get(String callId) {
        return callIdMap.get(callId);
    }

    public static String getDeviceId(String callId) {
        return callIdMap.get(callId, false).getDeviceId();
    }

    public static void putWithDeviceId(String callId, String deviceId) {
        callIdMap.put(callId, new TempData(deviceId, null, null, null));
    }

    public static void remove(String callId) {
        callIdMap.remove(callId);
    }
}
